package com.tunte.springcloudk8sdiscovery.controller;

import com.alibaba.fastjson.JSON;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class DiscoveryController {

	private static final Log log = LogFactory.getLog(DiscoveryController.class);

	@Autowired
	private DiscoveryClient discoveryClient;

	@RequestMapping("/")
	public String hello() {
		return "Hello World";
	}

	/**
	 * 返回远程调用的结果
	 * @return 服务详情
	 */
	@RequestMapping("/getservicedetail")
	public String getservicedetail(
			@RequestParam(value = "servicename", defaultValue = "") String servicename) {
		return "Service [" + servicename + "]'s instance list : "
				+ JSON.toJSONString(discoveryClient.getInstances(servicename));
	}

	@RequestMapping("/services")
	public List<String> services() {
		return this.discoveryClient.getServices();
	}

}
